import { _decorator, Component, Node, Prefab, instantiate, Vec3} from 'cc';

import { DirectorManager } from "./DirectorManager";
import { CardManager } from "./CardManager";
import { CpgStack } from "./CpgStack";

const { ccclass, property } = _decorator;

@ccclass('CpgCard') // 用 @ccclass 声明类为 Cocos 组件
export class CpgCard extends Component {

    @property([Node]) actionGroups: Node[] = [];

    private actionLevel = 0;

    public mySeat: boolean = false;

    onReset() {
        this.actionLevel = 0;
        for(let i = 0; i < this.actionGroups.length; i++)
        {
            this.actionGroups[i].active = false;
        }

        if(this.mySeat)
        {
            DirectorManager.instance.setFirstViewport(0);
        }
    }

    //no reset 
    takeAway() {
        const v3: Vec3 = this.node.position;
        if (Math.abs(Math.abs(v3.x) - 0.242) < 0.001 ) {
            v3.x = Math.sign(v3.x) * 0.295;
        } else if (Math.abs( Math.abs(v3.z) - 0.242) < 0.001) {
            v3.z = Math.sign(v3.z) * 0.295;
        }
        this.node.setPosition(v3);
    }

    notFullyCollected() : boolean {
        return this.actionLevel < this.actionGroups.length;
    }

    getLevel() : number {
        return this.actionLevel;
    }

    onAction() {

        if(this.notFullyCollected())
        {
            this.onPeng();
        }
    }

    onPeng() {

        const actionGroup = this.actionGroups[this.actionLevel];
        if(actionGroup)
        {
            actionGroup.active = true;
        }

        this.actionLevel++;

        // this.addStack();

        if(this.mySeat)
        {
            DirectorManager.instance.setFirstViewport(this.actionLevel);
        }

    }

    
    addStack() {

        const cardIDs = [311, 322, 333];
        const stackNode = CardManager.createStackCard(cardIDs);

        const stack = stackNode.getComponent(CpgStack);
        stack?.setData(cardIDs);
        
        const pos = new Vec3(0.095 * this.actionLevel++, 0, 0);
        stackNode.setPosition(pos);
        this.node.addChild(stackNode);
    }

}